package com.tian.controller;

import com.tian.convert.ChargeBalanceRecordReqDto;
import com.tian.dto.ChargeBalanceRecordDto;
import com.tian.dto.PayNotifyReqDto;
import com.tian.dto.WxPrePayReqDto;
import com.tian.enums.ResultCode;
import com.tian.service.ChargeBalanceRecordService;
import com.tian.util.CommonResult;
import com.tian.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author tianwc  公众号：java后端技术全栈、面试专栏
 * @version 1.0.0
 * @date 2023年05月12日 15:57
 * 博客地址：<a href="http://woaijava.cc/">博客地址</a>
 */
@Slf4j
@RestController
@RequestMapping(value = "/chargeBalanceRecord", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class ChargeBalanceRecordController {

    @Resource
    private ChargeBalanceRecordService chargeWxPayService;

    @PostMapping("/prePay")
    public CommonResult<Boolean> prePay(@RequestBody WxPrePayReqDto wxPrePayReqDto) {
        if (wxPrePayReqDto == null || wxPrePayReqDto.getUserId() == null || wxPrePayReqDto.getAmount() == null) {
            return CommonResult.failed(ResultCode.PARAMETER_EMPTY);
        }
        return chargeWxPayService.createRecord(wxPrePayReqDto);
    }

    @PostMapping("/selectByOrderNo")
    public CommonResult<ChargeBalanceRecordDto> selectByOrderNo(@RequestBody ChargeBalanceRecordReqDto chargeBalanceRecordReqDto) {
        if (chargeBalanceRecordReqDto == null || StringUtil.isBlank(chargeBalanceRecordReqDto.getOrderNo())) {
            return CommonResult.failed(ResultCode.PARAMETER_EMPTY);
        }
        return chargeWxPayService.selectByOrderNo(chargeBalanceRecordReqDto.getOrderNo());
    }

    @PostMapping("/query/pay/order/job")
    public CommonResult<Boolean> queryPayOrderJob() {
        return chargeWxPayService.queryPayOrderJob();
    }

    @PostMapping("/wx/notify")
    public CommonResult<Boolean> wxPayNotify(@RequestBody PayNotifyReqDto payNotifyReqDto) {
        try {
            return chargeWxPayService.wxPayNotify(payNotifyReqDto);
        } catch (Exception e) {
            log.error("会带通知失败，异常信息：", e);
            return CommonResult.failed(ResultCode.FAILED, e.getMessage());
        }
    }
}
